import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: wangxin
 * Date: 2024-12-21
 * Time: 17:03
 */
public class MyBitSet {
    public byte[] elem;
    public int userSize;
    public MyBitSet(){
        this.elem = new byte[1];
    }
    public MyBitSet(int n){
        this.elem = new byte[n/8+1];
    }
    public void set(int val){
        if(val < 0){
            throw new IndexOutOfBoundsException("设置的值不合法");
        }
        int elemIndex = val/8;
        int byteIndex = val%8;
        elem[elemIndex] |= (1 << byteIndex);
        userSize++;
    }
    public boolean get(int val){
        if(val < 0){
            throw new IndexOutOfBoundsException("设置的值不合法");
        }
        int elemIndex = val/8;
        if(elemIndex > elem.length -1){
            elem = Arrays.copyOf(elem,elemIndex+1);
        }
        int byteIndex = val%8;
        if((elem[elemIndex] & (1 << byteIndex)) != 0){
            return true;
        }
        return false;
    }
    public void reSet(int val){
        if(val < 0){
            throw new IndexOutOfBoundsException("设置的值不合法");
        }
        int elemIndex = val/8;
        int byteIndex = val%8;
        elem[elemIndex] &= ~(1<<byteIndex);
        userSize--;
    }
    public int getUserSize(){
        return userSize;
    }
}
